<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData" :rowCls="rowCls">
    <template v-if="!taskCode" v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeft="{ selected = [] }">
      <q-btn :disable="isDisabled(selected)" outline text-color="primary" unelevated @click="handleManual(selected[0])">
        {{ `${$t('Binding')}/${$t('Unbind')}` }}
      </q-btn>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickTips') }}</q-tooltip>
      </q-icon>
    </template>
  </qx-table>
  <q-modal v-model="modalVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="rowInfo" cols="2"></QInfo>
  </q-modal>
</template>
<script setup>
import { i18n, toI18nName } from 'boot/i18n.js'
import { postAPI } from 'src/api/index.js'
import { getRemoteData } from 'src/enum/ajaxRemoteData.js'
import { bindOrUnbind } from 'src/enum/index.js'
import { createDialog } from 'src/utils/utils.js'
import { rackDirections, updateModes } from 'src/views/CarrierCore/enum.js'
import { notificationStatus } from 'src/views/TaskCore/enum.js'
import { computed, onMounted, ref } from 'vue'

const props = defineProps({ taskCode: String })

const rowInfo = ref({})
const AGVBrands = ref([])
const searchForm = ref({})
const modalVisible = ref(false)
const remoteData = ref({ dicUrl: '/yfagv/api/bindPointShelvesOrder/list' })
getRemoteData('agv_brand', '/yfagv/api/agvCfgBrand/list').then(data => (AGVBrands.value = data))
const brandProps = computed(() => ({ label: `${toI18nName('agvBrandNameZhCn')}`, value: 'agvBrandCode' }))
const tableOption = computed(() => {
  return {
    span: 22, export: true, labelWidth: 70, searchSpan: 4, maxHeight: 'inherit',
    column: [{
      label: `${i18n.t('AGVCore.AGVBrand')}`, field: 'agvBrandCode', type: 'select', options: AGVBrands.value,
      props: brandProps.value, minWidth: 80
    }, {
      label: `${i18n.t('MapCore.StationName')}`, field: 'pointCode', minWidth: 80, required: true
    }, {
      label: `${i18n.t('CarrierCore.RackCode')}`, field: 'shelvesCode', minWidth: 80, required: true
    }, {
      label: `${i18n.t('CarrierCore.RackDirection')}`, field: 'shelvesDir', useTag: true,
      options: rackDirections.value, minWidth: 80, search: false, hide: true
    }, {
      label: `${i18n.t('TaskCore.RelatedTaskCode')}`, field: 'taskCode', minWidth: 80, display: false,
      labelWidth: 80
    }, {
      label: `${i18n.t('LogCore.OperType')}`, field: 'inBind', type: 'select', useTag: true,
      options: bindOrUnbind.value, minWidth: 80, search: false
    }, {
      label: `${i18n.t('Misc.UpdateMode')}`, field: 'updateMode', options: updateModes.value,
      minWidth: 80, search: false, hide: true
    }, {
      label: `${i18n.t('TaskCore.NotificationStatus')}`, field: 'notifyStatus', type: 'select',
      options: notificationStatus.value, minWidth: 80, useTag: true
    }, {
      label: `${i18n.t('StartTime')}`, field: 'notifyStartTime', minWidth: 80, search: false
    }, {
      label: `${i18n.t('EndTime')}`, field: 'notifyEndTime', minWidth: 80, search: false
    }, {
      label: `${i18n.t('Misc.ExceptionReason')}`, field: 'notifyExceptionMsg', minWidth: 80, search: false
    }, {
      label: `${i18n.t('Misc.RetryCount')}`, field: 'notifyRetryCnt', minWidth: 80, search: false,
      defaultValue: '0'
    }, {
      label: `${i18n.t('AppCore.AppCode')}`, field: 'appId', minWidth: 80, search: false
    }]
  }
})

const rowCls = ({ row }) => row.notifyStatus === '3' ? 'text-red-4' : ''
const rowDblClick = row => {
  rowInfo.value = row
  modalVisible.value = true
}
const isDisabled = (selected = []) => !selected[0] || ['2', '3'].includes(selected[0].notifyStatus)
const handleManual = row => {
  createDialog({ msg: `${i18n.t('Misc.ManualOperationTips')}` }, () => {
    postAPI(`/yfagv/api/bindPointShelvesOrder/manualInBind/${row.id}`, {}, () => startSearch(), () => {})
  }, () => {})
}
const formatSearchForm = () => {
  const params = { ...searchForm.value }
  if (props.taskCode) params.taskCode = props.taskCode
  if (searchForm.value.createdTime?.from) {
    params.createdStartTime = params.createdTime?.from
    params.createdEndTime = params.createdTime?.to
    delete params.createdTime
  }
  return { ...params }
}
const startSearch = (_, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
}

onMounted(startSearch)
</script>